package com.dianping.cat.home.dependency.graph.transform;

import com.dianping.cat.home.dependency.graph.IEntity;
import com.dianping.cat.home.dependency.graph.IVisitor;
import com.dianping.cat.home.dependency.graph.entity.TopologyEdge;
import com.dianping.cat.home.dependency.graph.entity.TopologyGraph;
import com.dianping.cat.home.dependency.graph.entity.TopologyNode;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;



















public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  38 */   public DefaultXmlBuilder() { this(false); }



/*  42 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  46 */     this.m_compact = compact;
/*  47 */     this.m_sb = sb;
/*  48 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  52 */     entity.accept(this.m_visitor);
/*  53 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  57 */     this.m_level--;

/*  59 */     indent();
/*  60 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  64 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  68 */     if (value == null) {
/*  69 */       return null;
    }

/*  72 */     String str = toString(value);
/*  73 */     int len = str.length();
/*  74 */     StringBuilder sb = new StringBuilder(len + 16);

/*  76 */     for (int i = 0; i < len; i++) {
/*  77 */       char ch = str.charAt(i);

/*  79 */       switch (ch) {
        case '<':
/*  81 */           sb.append("&lt;");
          break;
        case '>':
/*  84 */           sb.append("&gt;");
          break;
        case '&':
/*  87 */           sb.append("&amp;");
          break;
        case '"':
/*  90 */           if (!text) {
/*  91 */             sb.append("&quot;");
            break;
          }
        default:
          sb.append(ch);
          break;
      }

    }
/* 100 */     return sb.toString();
  }

  protected void indent() {
/* 104 */     if (!this.m_compact) {
/* 105 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 106 */         this.m_sb.append("   ");
      }
    }
  }


/* 112 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 116 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 120 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 124 */     indent();

/* 126 */     this.m_sb.append('<').append(name);

/* 128 */     int len = nameValues.length;

/* 130 */     for (int i = 0; i + 1 < len; i += 2) {
/* 131 */       Object attrName = nameValues[i];
/* 132 */       Object attrValue = nameValues[i + 1];

/* 134 */       if (attrValue != null) {
/* 135 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 139 */     if (dynamicAttributes != null) {
/* 140 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 141 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 145 */     if (text != null && closed) {
/* 146 */       this.m_sb.append('>');
/* 147 */       this.m_sb.append(escape(text, true));
/* 148 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 150 */       if (closed) {
/* 151 */         this.m_sb.append('/');
      } else {
/* 153 */         this.m_level++;
      }

/* 156 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 162 */     if (value instanceof String)
/* 163 */       return (String)value; 
/* 164 */     if (value instanceof Collection) {
/* 165 */       Collection<Object> list = (Collection)value;
/* 166 */       StringBuilder sb = new StringBuilder(32);
/* 167 */       boolean first = true;

/* 169 */       for (Object item : list) {
/* 170 */         if (first) {
/* 171 */           first = false;
        } else {
/* 173 */           sb.append(',');
        }

/* 176 */         if (item != null) {
/* 177 */           sb.append(item);
        }
      }

/* 181 */       return sb.toString();
/* 182 */     }  if (value.getClass().isArray()) {
/* 183 */       int len = Array.getLength(value);
/* 184 */       StringBuilder sb = new StringBuilder(32);
/* 185 */       boolean first = true;

/* 187 */       for (int i = 0; i < len; i++) {
/* 188 */         Object item = Array.get(value, i);

/* 190 */         if (first) {
/* 191 */           first = false;
        } else {
/* 193 */           sb.append(',');
        }

/* 196 */         if (item != null) {
/* 197 */           sb.append(item);
        }
      }

/* 201 */       return sb.toString();
    }

/* 204 */     return String.valueOf(value);
  }



/* 209 */   public void visitTopologyEdge(TopologyEdge topologyEdge) { startTag("topology-edge", true, null, new Object[] { "key", topologyEdge.getKey(), "type", topologyEdge.getType(), "target", topologyEdge.getTarget(), "opposite", Boolean.valueOf(topologyEdge.isOpposite()), "weight", Integer.valueOf(topologyEdge.getWeight()), "status", Integer.valueOf(topologyEdge.getStatus()), "des", topologyEdge.getDes(), "link", topologyEdge.getLink(), "self", topologyEdge.getSelf(), "dashed", Boolean.valueOf(topologyEdge.isDashed()) }); }



  public void visitTopologyGraph(TopologyGraph topologyGraph) {
/* 214 */     startTag("topology-graph", null, new Object[] { "id", topologyGraph.getId(), "type", topologyGraph.getType(), "status", Integer.valueOf(topologyGraph.getStatus()), "des", topologyGraph.getDes() });

/* 216 */     if (!topologyGraph.getNodes().isEmpty()) {
/* 217 */       for (TopologyNode topologyNode : topologyGraph.getNodes().values()) {
/* 218 */         topologyNode.accept(this.m_visitor);
      }
    }

/* 222 */     if (!topologyGraph.getEdges().isEmpty()) {
/* 223 */       for (TopologyEdge topologyEdge : topologyGraph.getEdges().values()) {
/* 224 */         topologyEdge.accept(this.m_visitor);
      }
    }

/* 228 */     endTag("topology-graph");
  }



/* 233 */   public void visitTopologyNode(TopologyNode topologyNode) { startTag("topology-node", true, null, new Object[] { "id", topologyNode.getId(), "type", topologyNode.getType(), "status", Integer.valueOf(topologyNode.getStatus()), "des", topologyNode.getDes(), "link", topologyNode.getLink(), "weight", Integer.valueOf(topologyNode.getWeight()) }); }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/dependency/graph/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */